#include "HTTP_GET.h"

String http_get(String url,u8 mode)
{
    WiFiClient client;
    WiFiClientSecure httpsClient;    //建立WiFiClientSecure对象
    HTTPClient http;
    u8 x;
    #ifdef DEBUG
    {
        Serial.print("conect to:");
        Serial.println(url);
    }
    #endif
    if(mode)
    {
        httpsClient.setInsecure();
        x = http.begin(httpsClient,url);
    }
    else
    {
        x = http.begin(client,url);
    }
    if (x)
    {
        #ifdef DEBUG
        {
            Serial.println("http conect success");
        }
        #endif
        int httpCode = http.GET();
        #ifdef DEBUG
        {
            Serial.println(httpCode);
        }
        #endif
        if(httpCode == HTTP_CODE_OK)
        {
            String payload = http.getString();
            #ifdef DEBUG
            {
                Serial.println(payload);
            }
            #endif
            client.stop();
            httpsClient.stop();
            http.end();
            return payload;
        }
        else
        {
            httpsClient.stop();
            client.stop();
            http.end();
            return "";

        }


    }
    else
    {
    #ifdef DEBUG
    {
        Serial.println("http conect fail");
    }
    #endif
    httpsClient.stop();
    client.stop();
    http.end();
    return "";
    }
}